package unitTest;


import static org.junit.Assert.*;
import java.sql.SQLException;
import library.*;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class TestBook{
	@Rule public ExpectedException thrown= ExpectedException.none();
	private String testingBookName = "testing book item";
	private BookController bookController;
	@Before
	public void setUp() throws SQLException{
		DaoUtil.init();
		bookController = new BookController();
	}
	
	@Test
	public void addBook() throws Exception {
		Book newBook = new Book(testingBookName,1234567);
		Boolean res =  bookController.createNewBook(newBook);
		assertTrue(res);
        bookController.getBookById(15);
	}
	
	@Test
	public void checkExistBook() throws Exception {
		Book bk2 = bookController.getBookByName(testingBookName);
		assertEquals(testingBookName, bk2.getName());
	}
	
	@Test
	public void checkNotExistBook() throws Exception {
		thrown.expect(Exception.class);
		thrown.expectMessage("Book id: -987 not found.");
		bookController.getBookById(-987);
	}
    @Test
    public void searchBook() throws Exception {
        Book bk = bookController.search("testing book item").get(0);
        assertEquals(bk.getName(), "testing book item");
    }
}
